################################################################################
# Workplace contact and support for anti-immigration parties
# Authors: Henrik Andersson and Sirus H. Dehdari
# Description: Randomizing workplace foreign-born shares across workplaces
################################################################################

# REMOVES ALL OBJECTS: rm(list = ls())

# We save paths for input and output:
input_temp <- "E://ProjData//IntegrationSD//Temp//"

library(readstata13) # Import dta-files
library(foreign) # Export dta-files

### Importing data: ----
workplace_2006 <-  read.dta13(paste(input_temp,"workplace_only_2006.dta", sep =""))
workplace_2010 <-  read.dta13(paste(input_temp,"workplace_only_2010.dta", sep =""))
workplace_2014 <-  read.dta13(paste(input_temp,"workplace_only_2014.dta", sep =""))


### Randomizing shares:

# We create 1000 versions of the last variable (share_im_cworker), and attach them randomly to
# the workplaces
set.seed(2006)
for (y in c(2006,2010,2014)){
  temp_data <- get(paste("workplace_",y,sep=""))
  random_temp <- as.data.frame(cbind(temp_data$ArbstId,replicate(1000,sample(temp_data$share_im_cworker))))
  colnames(random_temp) <- c("ArbstId",paste("share_im_cworker_rand",1:1000,sep=""))
  assign(paste("random_wpshares_",y,sep=""),random_temp)
  rm(random_temp, temp_data)
}  
  
  

write.dta(random_wpshares_2006,version = 12, paste(input_temp,"random_wpshares_2006.dta",sep=""))
write.dta(random_wpshares_2010,version = 12, paste(input_temp,"random_wpshares_2010.dta",sep=""))
write.dta(random_wpshares_2014,version = 12, paste(input_temp,"random_wpshares_2014.dta",sep=""))
  
  
  
  
  
  
















